package cn.exrick.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.exrick.common.exception.NeedLoginException;
import cn.exrick.common.exception.XmallException;
import cn.exrick.common.jedis.JedisClient;
import cn.exrick.pojo.TbAddress;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.front.Member;
import cn.exrick.service.AddressService;
import cn.exrick.service.LoginService;
import cn.exrick.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "收货地址")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private LoginService loginService;

    @Autowired
    private JedisClient jedisClient;

    @RequestMapping(value = "/member/addressList", method = RequestMethod.POST)
    @ApiOperation(value = "获得所有收货地址")
    public Result<List<TbAddress>> addressList(String token) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        List<TbAddress> list = addressService.getAddressList(member.getId());
        return new ResultUtil<List<TbAddress>>().setData(list);
    }

    @RequestMapping(value = "/member/address", method = RequestMethod.POST)
    @ApiOperation(value = "通过id获得收货地址")
    public Result<TbAddress> address(String token, Long addressId) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }

        TbAddress address = addressService.getAddress(addressId);
        if (address != null && address.getUserId() != member.getId()) {
            throw new XmallException("无权查看他人收货地址");
        }
        return new ResultUtil<TbAddress>().setData(address);
    }

    @RequestMapping(value = "/member/addAddress", method = RequestMethod.POST)
    @ApiOperation(value = "添加收货地址")
    public Result<Object> addAddress(String token, String userName, String tel, Boolean isDefault, String streetName) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        TbAddress tbAddress = new TbAddress();
        tbAddress.setUserId(member.getId());
        tbAddress.setUserName(userName);
        tbAddress.setTel(tel);
        tbAddress.setIsDefault(isDefault == null ? false : isDefault);
        tbAddress.setStreetName(streetName);
        int result = addressService.addAddress(tbAddress);

        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/member/updateAddress", method = RequestMethod.POST)
    @ApiOperation(value = "编辑收货地址")
    public Result<Object> updateAddress(String token, Long id, String userName, String tel, Boolean isDefault, String streetName) {
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        if (id == null) {
            throw new XmallException("参数不足");
        }
        TbAddress tbAddress = addressService.getAddress(id);
        if (tbAddress == null) {
            throw new XmallException("收货地址不存在");
        }
        if (tbAddress.getUserId() != member.getId()) {
            throw new XmallException("无权修改他人收货地址");
        }
        if (StringUtils.isNotEmpty(userName)) {
            tbAddress.setUserName(userName);
        }
        if (StringUtils.isNotEmpty(tel)) {
            tbAddress.setTel(tel);
        }
        if (StringUtils.isNotEmpty(streetName)) {
            tbAddress.setStreetName(streetName);
        }
        if (isDefault != null) {
            tbAddress.setIsDefault(isDefault);
        }
        int result = addressService.updateAddress(tbAddress);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/member/delAddress", method = RequestMethod.POST)
    @ApiOperation(value = "删除收货地址")
    public Result<Object> delAddress(String token, Long id) {
        if (id == null) {
            throw new XmallException("参数不足");
        }
        Member member = loginService.getUserByToken(token);
        if (member == null) {
            throw new NeedLoginException();
        }
        TbAddress tbAddress = addressService.getAddress(id);
        if (tbAddress == null) {
            throw new XmallException("收货地址不存在");
        }
        if (tbAddress.getUserId() != member.getId()) {
            throw new XmallException("无权删除他人收货地址");
        }
        int result = addressService.delAddress(tbAddress);
        return new ResultUtil<Object>().setData(result);
    }
}
